home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / make / icmake-6.000 / icmake-6 / icmake / pp / directiv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-01  |  5.1 KB  |  158 lines

  1. /*
  2. \funcref{directive}{void directive ()}
  3.     {}
  4.     {}
  5.     {error(), pushfile(), insert()}
  6.     {lexer()}
  7.     {directiv.c}
  8.     {
  9.         {\em directive()} is called from {\em lexer()} when a `\#' character is
  10.         read in the input file. {\em directive()} does not expect to read the
  11.         `\#' character. Three directives are recognized:
  12.  
  13.         \begin{itemize}
  14.  
  15.             \item {\bf \#} {\em [spaces]} {\bf include} {\em [spaces]} {\bf
  16.             "}{\em filename}{\bf "}. When this directive is read, {\em
  17.             pushfile()} is called to start processing the included file.
  18.  
  19.             Analogous to {\bf C}, an {\bf \#include} $<$...$>$ form is
  20.             supported.
  21.  
  22.             \item {\bf \#} {\em [spaces]} {\bf define} {\em [spaces]} {\em name}
  23.             {\em [spaces]} {\em [redefinition]}. When this directive is read,
  24.             {\em insert()} is called to insert the definition in the symbol
  25.             table. The defined {\em name} is passed to {\em insert()} as
  26.             argument; the redefinition is stored in the lexical buffer {\em
  27.             lexbuf}.
  28.  
  29.             \item {\bf \#\!} \ldots
  30.  
  31.             The line containing this directive is skipped. The directive is
  32.             intended to be used on {\sc unix} platforms, where it can be used
  33.             to execute an icmake script.
  34.  
  35.         \end{itemize}
  36.  
  37.         Other directives following the `\#' character lead to an error.
  38.     }
  39. */
  40.  
  41. #include "icm-pp.h"
  42.  
  43. void directive ()
  44. {
  45.     register int
  46.         imdir_used,
  47.         index,
  48.         ch;
  49.     static char
  50.         dirsep [2] = { DIRSEP, '\0' },
  51.         idname [200];
  52.     char
  53.         filename [_MAX_PATH];
  54.  
  55.                                             /* directive is #! */
  56.     if ((ch = fgetc (filestack [filesp].f)) == '!')
  57.     {                                       /* skip while not \n and not EOF */
  58.         while ((ch = fgetc (filestack [filesp].f)) != '\n' && ch != EOF)
  59.             ;
  60.  
  61.         if (ch == '\n')                     /* last char was newline */
  62.         {
  63.             filestack [filesp].l++;         /* then increment line counter */
  64.             fputc ('\n', outfile);          /* and empty line to output */
  65.         }
  66.  
  67.         return;
  68.     }
  69.                                             /* not #!, but maybe '#  ' */
  70.     while (ch == ' ' || ch == '\t')         /* skip all blanks */
  71.         ch = fgetc (filestack [filesp].f);
  72.  
  73.     ungetc (ch, filestack [filesp].f);      /* push back next char, not ' ' */
  74.  
  75.     if (! fgets (lexbuf, 8, filestack [filesp].f))
  76.         error ("%s: %d: bad preprocessor directive",
  77.                filestack [filesp].n, filestack [filesp].l);
  78.  
  79.     if (! strncmp (lexbuf, "include", 7))
  80.     {
  81.         imdir_used = 0;                 /* assume include "..." form */
  82.         while ( (ch = fgetc (filestack [filesp].f)) == ' ' ||
  83.                 ch == '\t'
  84.               )
  85.             ;
  86.         if (ch != '\"' && ch != '<')
  87.             error ("%s: %d: \" or < expected after #include directive",
  88.                    filestack [filesp].n, filestack [filesp].l);
  89.         if (ch == '<')                  /* include <...> form? */
  90.             imdir_used++;
  91.         index = 0;
  92.         while ( (ch = fgetc (filestack [filesp].f)) != '\"' && ch != '>')
  93.             if (ch == '\n' || ch == EOF)
  94.                 error ("%s: %d: unterminated name after #include directive",
  95.                        filestack [filesp].n, filestack [filesp].l);
  96.             else
  97.                 lexbuf [index++] = (char) ch;
  98.         lexbuf [index] = '\0';
  99.         while ( (ch = fgetc (filestack [filesp].f)) != '\n' &&
  100.                 ch != EOF)
  101.             ;
  102.         ungetc (ch, filestack [filesp].f);
  103.  
  104.         if (imdir_used)
  105.         {
  106.             strcpy (filename, imdir);
  107.             strcat (filename, dirsep);
  108.             strcat (filename, lexbuf);
  109.             pushfile (filename);
  110.         }
  111.         else
  112.             pushfile (lexbuf);
  113.     }
  114.     else if (! strncmp (lexbuf, "define", 6))
  115.     {
  116.         while                                   /* define read, skip blanks */
  117.         (
  118.             (ch = fgetc (filestack [filesp].f)) == ' '
  119.             ||
  120.             ch == '\t'
  121.         )
  122.             ;
  123.  
  124.         ungetc (ch, filestack [filesp].f);      /* push back first non-blank */
  125.  
  126.         getident (idname);                      /* get the name of the define */
  127.  
  128.         while                                   /* skip blanks again */
  129.         (
  130.             (ch = fgetc (filestack [filesp].f)) == ' '
  131.             ||
  132.             ch == '\t'
  133.         )
  134.             ;
  135.  
  136.         ungetc (ch, filestack [filesp].f);      /* and push-back non-blank */
  137.  
  138.         index = 0;                          /* read the line-remainder */
  139.         while
  140.         (
  141.             (ch = fgetc (filestack [filesp].f)) != '\n'
  142.             &&
  143.             ch != EOF
  144.         )
  145.             lexbuf [index++] = (char) ch;
  146.  
  147.         lexbuf [index] = '\0';              /* terminate the line */
  148.  
  149.         no_comment();                       /* remove comment from lexbuf */
  150.  
  151.         ungetc (ch, filestack [filesp].f);
  152.         insert (idname);
  153.     }
  154.     else
  155.         error ("%s: %d: bad preprocessor directive",
  156.                filestack [filesp].n, filestack [filesp].l);
  157. }
  158.